package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("travel")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyService strategyService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") TravelQuery qo, Model model, HttpSession session){
        qo.setState(Travel.STATE_WAITING);
        //只有公开的才能查询
        qo.setIsPublic(true);
        //根据更新时间排序
        qo.setOrderBy("t.lastUpdateTime desc");
        model.addAttribute("result",travelService.selectForList(qo));
        return "travel/list";
    }
    //已发布列表
    @RequestMapping("releaseList")
    public String releaseList(@ModelAttribute("qo") TravelQuery qo, Model model, HttpSession session){
        qo.setState(Travel.STATE_RELEASE);
        //只有公开的才能查询
        qo.setIsPublic(true);
        //根据更新时间排序
        qo.setOrderBy("t.releaseTime desc");
        model.addAttribute("result",travelService.selectForList(qo));
        return "releaseTravel/list";
    }
    @RequestMapping("rejectList")
    public String rejectList(@ModelAttribute("qo") TravelQuery qo, Model model, HttpSession session){
        qo.setState(Travel.STATE_REJECT);
        //只有公开的才能查询
        qo.setIsPublic(true);
        //根据更新时间排序
        qo.setOrderBy("t.releaseTime desc");
        model.addAttribute("result",travelService.selectForList(qo));
        return "rejectTravel/list";
    }

    @RequestMapping("changeState")
    @ResponseBody
    public JSONResult changeState(Travel travel, Long authorId){
        JSONResult result = new JSONResult();
        try {
            travelService.changeState(travel, authorId);
        }catch (Exception e){
            result.mark(e.getMessage());
        }
        return result;
    }

    //高级查询,通过游记id查询关联攻略回显列表
    @RequestMapping("selectByTravelId")
    @ResponseBody
    public Travel selectByTravelId(Long travelId){
        Travel travel = travelService.selectByTravelId(travelId);
        return travel;
    }

    //添加攻略关联游记回显
    @RequestMapping("getRegionTravelByStrategyId")
    @ResponseBody
    public Object getRegionTravelByStrategyId(Long strategyId,Model model){
        List<Travel> travel = travelService.getRegionTravelByStrategyId(strategyId);
        return travel;
    }


}
